package designPattern.action11.Observer.chapter5_observer.chapter5_2;

import java.util.ArrayList;

public class SeekJobCenter implements Subject {
    String mess;
    boolean changed;
    ArrayList<Observer> personList;

    SeekJobCenter() {
        personList = new ArrayList<Observer>();
        mess = "";
        changed = false;
    }

    public void addObserver(Observer o) {
        if (!(personList.contains(o)))
            personList.add(o);
    }

    public void deleteObserver(Observer o) {
        if (personList.contains(o))
            personList.remove(o);
    }

    public void notifyObservers() {
        if (changed) {
            for (int i = 0; i < personList.size(); i++) {
                Observer observer = personList.get(i);
                observer.hearTelephone(mess);//推方式，直接把消息也推给观察者
            }
            changed = false;
        }
    }

    public void giveNewMess(String str) {
        if (str.equals(mess))
            changed = false;
        else {
            mess = str;
            changed = true;
        }
    }
}
